package com.guli.auth.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.client.utils.StringUtils;
import com.guli.auth.feign.FeignMember;
import com.guli.common.utils.Constant;
import com.guli.common.utils.R;
import com.guli.common.vo.MemberTo;
import com.guli.common.vo.ThriendVo;
import com.guli.common.vo.WeiboVo;
import com.guli.common.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;


/**
 * @author WZH
 * @title: AuthController
 * @projectName gulimall-java
 * @description:
 * @date 2022/1/13
 */
@Controller
public class AuthController {
    @Autowired
    private FeignMember feignMember;

    @GetMapping(value = {"/","login.html"})
    public String login(){

        return "login";
    }

    @GetMapping(value = {"/goLogin"})
    public String goLogin(@RequestParam String username,@RequestParam String password,HttpSession session){
        R r=feignMember.login(username,password);
        if (r.getCode()!=0){
            return null;
        }
        MemberTo member = r.getData(new TypeReference<MemberTo>(){});
        //存入session
        session.setAttribute("member",member);
        return "redirect:http://gulimall.com";
    }
    @GetMapping(value = {"register"})
    public String register(){

        return "register";
    }

    @RequestMapping(value = {"/auth/toRegister"})
    public String toRegister(MemberTo memberTo,HttpSession session){
        ThriendVo thriendVo= (ThriendVo) session.getAttribute("thriendVo");
        if (thriendVo!=null&&StringUtils.isNotBlank(thriendVo.getAccess_token())){
            memberTo.setAccess_token(thriendVo.getAccess_token());
            memberTo.setUid(thriendVo.getUid());
            memberTo.setExpires_in(thriendVo.getExpires_in());
        }
        memberTo.setCreateTime(new Date());
        feignMember.add(memberTo);
        session.setAttribute("member",memberTo);
        return "redirect:http://auth.gulimall.com";
    }

    @RequestMapping("oauth2.0/weibo/success")
    public String returns(@RequestParam String code, Model model, HttpSession session) throws Exception {
        //?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
        HashMap<String, String> map = new HashMap<>();
        map.put("client_id","4127204613");
        map.put("client_secret","2588fc2cd486d4e444a1142ea70a6ae3");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", null, map, "");
        //将第三方响应的数据转为json字符串
        String jsonToString = EntityUtils.toString(response.getEntity());
        ThriendVo thriendVo = JSONObject.parseObject(jsonToString, new TypeReference<ThriendVo>() {
        });
        session.setAttribute("thriendVo",thriendVo);
        //逻辑判断 存在直接登录 不存在先跳转注册
        MemberTo member=feignMember.getMemberByToken(thriendVo.getAccess_token());
        if (member!=null){
            //存入session
            session.setAttribute("member",member);
            return "redirect:http://gulimall.com";
        }
        //访问第三方获取基本信息-》进行注册
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("access_token",thriendVo.getAccess_token());
        map2.put("uid",thriendVo.getUid());
        HttpResponse response2 = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "get", null, map2);
        String json2ToString = EntityUtils.toString(response2.getEntity());
        WeiboVo weiboVo = JSONObject.parseObject(json2ToString, WeiboVo.class);
        session.setAttribute("weiboVo",weiboVo);
        MemberTo memberTo = new MemberTo();
        memberTo.setAccess_token(thriendVo.getAccess_token());
        memberTo.setUid(thriendVo.getUid());
        memberTo.setExpires_in(thriendVo.getExpires_in());
        memberTo.setUsername(weiboVo.getScreen_name());
        memberTo.setCity(weiboVo.getCity());
        memberTo.setCreateTime(new Date());
        memberTo.setPassword("111111");
        feignMember.add(memberTo);

        //存入session
        session.setAttribute("member",memberTo);
        return "redirect:http://gulimall.com";
    }


}
